using DevExpress.Xpo;
using PostSharp.CodeModel;

namespace XpoOneWayMultiAssociationWeaver
{
   internal static class Utils
   {
      /// <summary>
      /// Creates an association attribute.
      /// </summary>
      /// <param name="module">The module.</param>
      /// <param name="associationName">Name of the association.</param>
      /// <returns></returns>
      public static CustomAttributeDeclaration CreateAssociationAttribute(ModuleDeclaration module, string associationName)
      {
         var constructor =
            module.FindMethod(typeof(AssociationAttribute).GetConstructor(new[] { typeof(string) }),
                              BindingOptions.Default);
         var attribute = new CustomAttributeDeclaration(constructor);
         SerializationType serializationType = SerializationType.GetSerializationType(
            module.FindType(typeof(string), BindingOptions.Default));
         var serializedValue = new SerializedValue(serializationType, associationName);
         attribute.ConstructorArguments.Add(new MemberValuePair(MemberKind.Parameter, 0, "name", serializedValue));
         return attribute;
      }
   }
}